package action;

import bean.Vip;
import dao.VipDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "VipServlet")
public class VipServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        if (method == null || method.equals("query")) {
            List<Vip> vip= VipDao.selvip();
            request.setAttribute("list1", vip);
            request.getRequestDispatcher("hy.jsp").forward(request, response);
        } else if (method.equals("delete")) {
            String id = request.getParameter("id");
            VipDao.del(id);
            response.sendRedirect("VipServlet");
        }
        else if (method.equals("huanhuan")) {
            String id = request.getParameter("id");
            Vip shop = VipDao.upda(id);

            request.setAttribute("list", shop);
            request.getRequestDispatcher("updatevip.jsp").forward(request,
                    response);
        } else if (method.equals("upd")) {
            System.out.println("修改");
            Vip shop = new Vip();
            VipDao dao = new VipDao();

            int id = Integer.parseInt(request.getParameter("id"));
            System.out.println(id);
            response.setCharacterEncoding("utf-8");
            String no = request.getParameter("num");
            String name = request.getParameter("name");
            int address = Integer.parseInt(request.getParameter("gender"));
            int time = Integer.parseInt(request.getParameter("age"));
            int type = Integer.parseInt(request.getParameter("onum"));
            shop.setId(id);
            shop.setNum(no);
            shop.setName(name);
            shop.setGender(address);
            shop.setAge(time);
            shop.setOnum(type);
            // String name2=new String(name.getBytes("ISO-8859-1"),"utf-8");
            System.out.println(shop);
            dao.upda1(shop);
            response.sendRedirect("VipServlet");

        }
        else if (method.equals("insert")) {
            System.out.println("增加");
            response.setCharacterEncoding("utf-8");
            String no = request.getParameter("num");
            String name = request.getParameter("name");
            int address = Integer.parseInt(request.getParameter("gender"));
            int time = Integer.parseInt(request.getParameter("age"));
            int type = Integer.parseInt(request.getParameter("onum"));
            VipDao.insert(no, name, time, address, type);
            response.sendRedirect("VipServlet");

            // }else if (method.equals("test")) {
            // response.setCharacterEncoding("utf-8");
            // PrintWriter pw=response.getWriter();
            //
            // pw.write("jintisajf");
            // pw.close();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
